<div id="emailGrid"></div>
<script>
	var initEmailGrid = function() {
		var columns = [{
			title : '用户名',
			name : 'username',
			width : 150
		}, {
			title : '密码',
			name : 'password',
			width : 150
		}, {
			title : 'SMTP地址',
			name : 'smtpAddress',
			width : 150
		}, {
			title : 'SMTP端口',
			name : 'smtpPort',
			width : 120
		}, {
			title : '是否可用',
			name : 'usable',
			width : 80,
			render : function(item, name, index) {
				return item[name] ? '<span class="glyphicon glyphicon-ok" style="color:#5CB85C;margin-left:15px;"></span>' : '<span  class="glyphicon glyphicon-remove" style="color:#D9534F;margin-left:15px;"></span>';
			}
		}, {
			title : '是否默认邮箱',
			name : 'systemDefault',
			width : 150,
			render : function(item, name, index) {
				return item[name] ? '<span class="glyphicon glyphicon-ok" style="color:#5CB85C;margin-left:15px;"></span>' : '<span class="glyphicon glyphicon-remove" style="color:#D9534F;margin-left:15px;"></span>';
			}
		}, {
			title : '测试可用性',
			name : 'smtpPort',
			width : 100,
			render : function(item, name, index) {
				return '<a onclick="emailManager.testUsable(' + item.id + ', ' + index + ')"><span id="usable' + index + '" class="glyphicon glyphicon-transfer"></span>&nbsp;测试可用性</a>';
			}
		}, {
			title : '设置',
			name : 'smtpPort',
			width : 80,
			render : function(item, name, index) {
				return '<a onclick="emailManager.setDefaultEmail(' + item.id + ', ' + index + ')">设为默认配置</a>';
			}
		}];
		var buttons = [{
			content : '<button class="btn btn-primary" type="button"><span class="glyphicon glyphicon-plus"><span>添加</button>',
			action : 'add'
		}, {
			content : '<button class="btn btn-success" type="button"><span class="glyphicon glyphicon-edit"><span>修改</button>',
			action : 'modify'
		}, {
			content : '<button class="btn btn-danger" type="button"><span class="glyphicon glyphicon-remove"><span>删除</button>',
			action : 'delete'
		}];
		$.get('emailconfiguration/findall.koala').done(function(data) {
			$('#emailGrid').data('koala.grid', null).off().empty().grid({
				identity : 'id',
				columns : columns,
				buttons : buttons,
				isShowPages : false,
				isUserLocalData : true,
				localData : data
			}).on({
				'add' : function() {
					emailManager.add($(this));
				},
				'modify' : function(e, data) {
					var indexs = data.data;
					var $this = $(this);
					if (indexs.length == 0) {
						$this.message({
							type : 'warning',
							content : '请选择一条记录进行修改'
						});
						return;
					}
					if (indexs.length > 1) {
						$this.message({
							type : 'warning',
							content : '只能选择一条记录进行修改'
						});
						return;
					}
					emailManager.update($this, data.item[0]);
				},
				'delete' : function(e, data) {
					var indexs = data.data;
					var $this = $(this)
					if (indexs.length == 0) {
						$this.message({
							type : 'warning',
							content : '请选择要操作的记录'
						});
						return;
					}
					$this.confirm({
						content : '确定要删除所选记录吗?',
						callBack : function() {
							emailManager.del($this, indexs);
						}
					});
				}
			});
		});
	}
	$(function() {
		initEmailGrid();
	}); 
</script>
